package org.wyy.vm.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import org.wyy.vm.entity.Video;
import org.wyy.vm.service.IVideoService;
import org.wyy.vm.service.impl.VideoServiceImpl;
import javax.swing.JScrollPane;

public class VideoList extends JFrame {

	private static final long serialVersionUID = 1774207414580769018L;

	private IVideoService videoService = new VideoServiceImpl();

	private JPanel contentPane;
	private JTable table;

	/**
	 * Create the frame.
	 */
	public VideoList() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(VideoList.class.getResource("/res/head_8.png")));
		setTitle("视频管理");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 610, 510);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("视频");
		menuBar.add(mnNewMenu);

		JMenuItem mntmNewMenuItem = new JMenuItem("添 加");
		mntmNewMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new VideoAdd().setVisible(true);
				close();
			}
		});
		mnNewMenu.add(mntmNewMenuItem);

		JMenuItem mntmNewMenuItem_2 = new JMenuItem("删 除");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				Long videoId = getSelectedVideoId();
				if (videoId == null) {

					JOptionPane.showMessageDialog(getThis(), "请选择一个视频", "提示信息", JOptionPane.WARNING_MESSAGE);

				} else {
					int opt = JOptionPane.showConfirmDialog(getThis(), "是否确认删除", "确认信息", JOptionPane.YES_NO_OPTION);

					if (opt == JOptionPane.YES_OPTION) {

						refresh();
					}
				}

			}
		});
		mnNewMenu.add(mntmNewMenuItem_2);

		JMenuItem mntmNewMenuItem_3 = new JMenuItem("修 改");
		mntmNewMenuItem_3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Long videoId = getSelectedVideoId();
				if (videoId == null) {

					JOptionPane.showMessageDialog(getThis(), "请选择一个视频", "提示信息", JOptionPane.WARNING_MESSAGE);

				} else {
					int opt = JOptionPane.showConfirmDialog(getThis(), "是否确认修改", "确认信息", JOptionPane.YES_NO_OPTION);

					if (opt == JOptionPane.YES_OPTION) {
						Video video = videoService.getById(videoId);
						new VideoUpdate(video).setVisible(true);
						close();
					}
				}

			}
		});
		mnNewMenu.add(mntmNewMenuItem_3);

		JMenuItem mntmNewMenuItem_4 = new JMenuItem("查 看");
		mntmNewMenuItem_4.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Long videoId = getSelectedVideoId();
				if (videoId == null) {

					JOptionPane.showMessageDialog(getThis(), "请选择一个视频", "提示信息", JOptionPane.WARNING_MESSAGE);

				} else {

					Video video = videoService.getById(videoId);
					new VideoDetail(video).setVisible(true);

				}

			}
		});
		mnNewMenu.add(mntmNewMenuItem_4);

		JMenu mnNewMenu_1 = new JMenu("帮助");
		menuBar.add(mnNewMenu_1);

		JMenuItem mntmNewMenuItem_1 = new JMenuItem("关  于");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new About().setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_1);

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		table = createTable();
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(0, 0, 590, 440);
		contentPane.add(scrollPane);
	}

	private JTable createTable() {

		Object[] columnNames = videoService.getJTableColumnNames();
		Object[][] data = videoService.getJTableData();

		JTable table = new JTable();
		table.setModel(new DefaultTableModel(data, columnNames) {
			private static final long serialVersionUID = -7366873983824134445L;

			public boolean isCellEditable(int row, int column) {
				return false;
			}
		});
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setBounds(0, 0, 590, 440);

		return table;
	}

	private Long getSelectedVideoId() {
		int row = table.getSelectedRow();
		if (row == -1) {
			return null;
		}
		return (Long) table.getValueAt(row, 0);
	}

	private void close() {
		dispose();
	}

	private JFrame getThis() {
		return this;
	}

	private void refresh() {
		this.dispose();
		VideoList videoList = new VideoList();
		videoList.setVisible(true);
	}
}
